﻿
using RTW.DAL;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.Common.Servers
{
    public class CarMaintenanceServers
    {
        private readonly IRepository<vehicle_maintenance> vehicle_maintenanceRepository;

        public CarMaintenanceServers()
        {
            vehicle_maintenanceRepository = new DapperRepository<vehicle_maintenance>();

        }

        public List<vehicle_maintenance> GetData(string v_id,string vm_type)
        {
            var datas = vehicle_maintenanceRepository.GetAllEnumerable();

            if (!string.IsNullOrEmpty(v_id))
            {
                datas = datas.Where(m => m.V_ID == int.Parse(v_id));
            }
            if (!string.IsNullOrEmpty(vm_type))
            {
                datas = datas.Where(m => m.VM_TYPE == vm_type);
            }
            return datas.ToList();
        }

        public vehicle_maintenance GetModelById(string id)
        {
            return vehicle_maintenanceRepository.GetById(id);
        }


        internal void Add(vehicle_maintenance model)
        {
            vehicle_maintenanceRepository.Insert(model);
        }

        internal void Update(vehicle_maintenance model)
        {
            vehicle_maintenanceRepository.Update(model);
        }

        internal void Del(string id)
        {
            vehicle_maintenanceRepository.DeleteByIds(id);
        }


        internal List<SelectListItem> GetV_ID()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = vehicle_maintenanceRepository.SqlWithParamsDynamic("select ATTACHMENT_ID Value,concat(ATTACHMENT_ID,'-----',ATTACHMENT_NAME) Text from [dbo].[vehicle_car] where ATTACHMENT_ID Is not null and ATTACHMENT_NAME Is not null");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }



        internal List<SelectListItem> GetVM_TYPE()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = vehicle_maintenanceRepository.SqlWithParamsDynamic("select CODE_NO Value,concat(CODE_NO,'-----',CODE_NAME) Text  from [dbo].[sys_code] where PARENT_NO=17");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }



        internal List<SelectListItem> GetVM_PERSON()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = vehicle_maintenanceRepository.SqlWithParamsDynamic("select CODE_NO Value,concat(CODE_NO,'-----',CODE_NAME) Text  from [dbo].[sys_code] where PARENT_NO=99");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }


    }
}